package com.xiaoq.positionmarket.repository.impl;

import com.xiaoq.positionmarket.entity.PositionMarketEntity;
import com.xiaoq.positionmarket.repository.IPositionMarketRepository;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chenjh on 2018/4/2.
 */
@Repository
public class PositionMarketRepositoryImpl implements IPositionMarketRepository {

    @Autowired
    private Datastore datastore;

    @Override
    public boolean save(PositionMarketEntity entity) {
        if(entity == null || StringUtils.isEmpty(entity.getId())){
            return false;
        }
        datastore.save(entity);
        return true;
    }

    @Override
    public PositionMarketEntity getById(String id) {
        Query<PositionMarketEntity> positionMarketQuery = datastore.createQuery(PositionMarketEntity.class)
                .filter("_id", id);
        return positionMarketQuery.get();
    }

    @Override
    public boolean deleteById(String id) {
        if(StringUtils.isEmpty(id)){
            return false;
        }
        Query<PositionMarketEntity> positionMarketQuery = datastore.createQuery(PositionMarketEntity.class)
                .filter("_id", id);
        UpdateOperations<PositionMarketEntity> updateOperations = datastore.createUpdateOperations(PositionMarketEntity.class);
        updateOperations.set("deleteTime", System.currentTimeMillis());
        datastore.update(positionMarketQuery, updateOperations);
        return true;
    }

    @Override
    public List<PositionMarketEntity> getPositionMarketListByPage(String shopId, PagingVo pagingVo) {
        Query<PositionMarketEntity> positionMarketQuery = datastore.createQuery(PositionMarketEntity.class)
                .filter("shopId", shopId)
                .field("deleteTime").doesNotExist()
                .order(Sort.descending("createdAt"));

        if(pagingVo != null){
            positionMarketQuery.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
        }

        return positionMarketQuery.asList();
    }

    @Override
    public long getPositionMarketCountByPage(String shopId) {
        Query<PositionMarketEntity> positionMarketQuery = datastore.createQuery(PositionMarketEntity.class)
                .filter("shopId", shopId)
                .field("deleteTime").doesNotExist();
        return positionMarketQuery.count();
    }


    @Override
    public boolean updateById(PositionMarketEntity entity) {
        if(entity == null || StringUtils.isBlank(entity.getId())){
            return false;
        }
        Query<PositionMarketEntity> positionMarketQuery = datastore.createQuery(PositionMarketEntity.class)
                .filter("_id", entity.getId());
        UpdateOperations<PositionMarketEntity> updateOperations = datastore.createUpdateOperations(PositionMarketEntity.class);
        updateOperations.set("updatedAt", System.currentTimeMillis());
        if(entity.getTriggerRealTime() != null){
            updateOperations.set("triggerRealTime", entity.getTriggerRealTime());
        }
        datastore.update(positionMarketQuery, updateOperations);
        return true;
    }
}
